<?php
declare (strict_types = 1);

namespace app\common\service;

use think\facade\Session;
use think\facade\Cookie;
use think\facade\Request;
use think\facade\Db;
use app\common\model\AdminDynamic as M;
use app\common\validate\AdminDynamic as V;

class AdminDynamic
{
    public static function goAdd($data){
        $data['create_time'] = strtotime($data['create_time']);
        $validate = new V;
        if(!$validate->scene('add')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
            M::create($data);
            return ['msg'=>'发布动态成功'];
        }catch (\Exception $e){
            return ['msg'=>'发布动态失败'.$e->getMessage(),'code'=>201];
        }
    }


    public static function goEdit($data,$id){
        halt($data);
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
            $model = M::find($id);

            $model->title = $data['title'];
            $model->content = $data['content'];
            $model->save();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    public static function goRemove($id){
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->destroy($id);
            return ['msg'=>'删除动态成功'];
        }catch (\Exception $e){
            return ['msg'=>'删除动态失败'.$e->getMessage(),'code'=>201];
        }
    }
}